//
//  DWDNewfeatureViewController.swift
//  dwdWb
//
//  Created by Dengdwd on 15/10/10.
//  Copyright © 2015年 dwd. All rights reserved.
//

import UIKit

private let DWDReuseIdentifier = "DWDReuseIdentifier"
private let NewfeatureConut = 4
class DWDNewfeatureViewController: UICollectionViewController {
    
    init() {
    super.init(collectionViewLayout: UICollectionViewFlowLayout())
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareLayout()
        
        self.collectionView!.registerClass(NewfeatureCell.self, forCellWithReuseIdentifier: DWDReuseIdentifier)

        // Do any additional setup after loading the view.
    }
    private func prepareLayout() {
    let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
    layout.itemSize = view.size
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
    collectionView?.showsHorizontalScrollIndicator = false
    collectionView?.bounces = false
    collectionView?.pagingEnabled = true
        
    }




    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return NewfeatureConut
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(DWDReuseIdentifier, forIndexPath: indexPath) as! NewfeatureCell
    
        cell.iconIndex = indexPath.item
        
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let indexP = collectionView.indexPathsForVisibleItems().last!
        if indexP.item == NewfeatureConut - 1 {
        let cell = collectionView.cellForItemAtIndexPath(indexP) as! NewfeatureCell
        cell.startLoading()
        }
    }
}
private class NewfeatureCell:UICollectionViewCell {

    var iconIndex:NSInteger = 0 {
        didSet {
        self.iconView.image = UIImage(named: "new_feature_\(iconIndex + 1)")
        startBtn.hidden = true
        }
    }
    func startLoading() {
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
        }
    }
    lazy var iconView = UIImageView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpUI()
    }
    func setUpUI() {
        addSubview(iconView)
        addSubview(startBtn)
        iconView.frame = bounds
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy:NSLayoutRelation.Equal , toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -150))
        
    }
    lazy var startBtn : UIButton = {
    
        let btn = UIButton()
    
        btn.setTitle("开始体验", forState: UIControlState.Normal)
    
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
    
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
    
        btn.sizeToFit()
        
        btn.addTarget(self, action: "clickBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    @objc func clickBtn() {
        NSNotificationCenter.defaultCenter().postNotificationName(DWDSwiftRootviewControllerNotification, object: nil)        
    }
}

